package com.liuguang.rabbitmq.comsumer;

import com.liuguang.common.constans.CommonConstans;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ClassName: DeadLetterListener
 * @Description：
 * @Author: wanghr
 * Date: 6:11 PM 2020/11/5
 * Version: 1.0
 */
@Component
public class DeadLetterListener {
    private static final Logger logger = LoggerFactory.getLogger(DeadLetterListener.class);

    /**
     * 处理死信队列消息
     */
    @RabbitListener(queues = {CommonConstans.DEAD_LETTER_QUEUE})
    public void handleDeadLetter(Message message, Channel channel) throws IOException {
        String messageBody = new String(message.getBody());
        logger.info("{}", messageBody);
        try {
            //处理私信消息
            logger.info("处理死信消息");
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            return;
        }
        logger.info("{},死信消息处理成功",messageBody);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }
}
